#####################################################################
# Public Preferences for Intergroup Assistance in Conflicts Facing  #
# Joint External Threats: Lessons from COVID-19 in Israel           #
# Liran Harsgor and Alon Yakter                                     #
# Journal of Conflict Resolition                                    #
#                                                                   #
# Replication code - Figure 1                                       #
#####################################################################
# To load the data, users should place the relevant csv	file in     #
# in their R working directory (or type the full file paths as      #
# saved on their computer).                                         #
#####################################################################

## Required packages
library(foreign)
library(dplyr)
library(zoo)
library(ggplot2)
library(patchwork)
library(Rcpp)
library(magick)
library(ggpattern)

## load data
dat <- read.csv("Israel and Palestine COVID-19 data.csv")
dat$date <- as.Date(dat$date, format = "%m/%d/%Y")

####################
# Graph (Figure 1) #
####################
plot <- ggplot(dat, aes(x = date, y = new_cases_smoothed_per_million, linetype = location)) +
  annotate("rect",xmin=as.Date("2020-04-19"),xmax=as.Date("2020-04-20"),ymin=0,ymax=800, fill="grey", alpha=0.6) +
  annotate("rect",xmin=as.Date("2020-07-14"),xmax=as.Date("2020-07-20"),ymin=0,ymax=800, fill="grey", alpha=0.6) +
  annotate("rect",xmin=as.Date("2020-10-08"),xmax=as.Date("2020-10-19"),ymin=0,ymax=800, fill="grey", alpha=0.6) +
  annotate(geom = "text", x = as.Date("2020-04-23"), y = 780, label = "IDI Survey", hjust = "left") +
  annotate(geom = "text", x = as.Date("2020-07-23"), y = 750, label = "Wave I\n(Conjoint)", hjust = "left") +
  annotate(geom = "text", x = as.Date("2020-10-22"), y = 780, label = "Wave II", hjust = "left") +
  geom_line() +
  scale_x_date(date_labels = "%b %d", date_breaks = "2 months", 
               limits = c(as.Date("2020-02-20"), as.Date("2020-12-31"))) +
  ylim(-35,800) +
  labs(x = "Date", y = "New Confirmed Cases Per Million People\n(7-Day Smoothed)") +
  scale_linetype_discrete(guide = guide_legend(override.aes = list(pattern = "none")), 
                          labels = c("Israel", "West Bank and Gaza")) +
  theme_bw() +
  theme(
    panel.grid = element_blank(),
    legend.position = "bottom",
    legend.margin = margin(-6,-6,-6,-6),
    legend.title = element_blank(),
    legend.key = element_rect(color = "white")
  )

ggsave(file = "Figure 1.pdf", plot=plot, width=7, height=4.5)
